#include<iostream>
#include<cstdio>
#include<pthread.h>
#include<cstring>
#include<unistd.h>
#include<sys/types.h>
class Task
{
    public:
    Task(int a,int b):_a(a),_b(b){}
    ~Task(){}
    int Execute()
    {
        return _a+_b;
    }
    private:
    int _a;
    int _b;
};
class Result
{
    public:
    Result(int result):_result(result){}
    ~Result(){};
    int GetResult()
    {
        return _result;
    }
    private:
    int _result;
};
void* routin(void* n)
{
    Task* n3=(Task*)n;
    //Task* n2=static_cast<Task*>(n);
    Result* nu=new Result(n3->Execute());
    return nu;
}

int main()
{
    Task* n1=new Task(4,6);
    pthread_t id;
    int n=pthread_create(&id,nullptr,routin,n1);
    (void)n;
    //回收
    Result* ret=nullptr;
    int ret1=pthread_join(id,(void**)&ret);//返回Result*拿二级指针接受
    if(ret1==0)
    {
        std::cout<<"回收成功！结果是："<<ret->GetResult()<<std::endl;
    }
    else
    {
        std::cout<<"回收失败 error: "<<strerror(ret1)<<std::endl;
    }
}



/* std::string Formatid(const pthread_t& ar);
void* threadrun(void* atr)
{
    std::string name=(const char*)atr;
    int cunt=5;
    while(cunt)
    {
        std::cout<<"我是新线程，name:"<<name<<"，ID: "<<Formatid(pthread_self())<<std::endl;
        sleep(1);
        cunt--;
    }

    return (void*)123;
}
std::string Formatid(const pthread_t& ar)
{
    char arr[62];
    snprintf(arr,sizeof(arr),"0x%lx",ar);
    return arr;
}
int main()
{
    pthread_t id;
    pthread_create(&id,nullptr,threadrun,(void *)"thread_1");
    
    int cunt=5;
    while(cunt)
    {
        std::cout<<"我是主线程，我的ID："<<Formatid(pthread_self())<<std::endl;
        sleep(1);
        cunt--;
    }
    //回收新线程
    //新线程返回值是一个void*
    void*ret;
    pthread_join(id,&ret);
    std::cout<<"新线程回收成功！退出码："<<(unsigned long long int)ret<<std::endl;
    return 0;
}
 */